/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.web.action.topo;

import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.sinodata.bsm.center.cache.TopuCatalogCache;
import com.sinodata.bsm.center.cache.TopuConfCache;
import com.sinodata.bsm.center.service.topo.TopoCatalogService;
import com.sinodata.bsm.center.util.web.Struts2Utils;
import com.sinodata.bsm.center.web.action.BaseAction;
import com.sinodata.bsm.common.vo.TopuCatalog;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author sunxiangxue
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-6-12 下午3:29:32          sunxiangxue        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class TopoCatalogAction extends BaseAction {

    Logger logger = Logger.getLogger(TopoAction.class);

    private TopuCatalog topuCatalog;

    @Autowired
    private TopoCatalogService topoCatalogService;

    @Autowired
    private TopuCatalogCache topuCatalogCache;//缓存对象

    @Autowired
    private TopuConfCache topuConfCache;//拓扑缓存对象

    private static final String RETURN_SUCCESS = "success";

    public TopuCatalog getTopuCatalog() {
        return topuCatalog;
    }

    public void setTopuCatalog(TopuCatalog topuCatalog) {
        this.topuCatalog = topuCatalog;
    }

    /* (non-Javadoc)
     * @see com.opensymphony.xwork2.ModelDriven#getModel()
     */
    @Override
    public Object getModel() {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see com.sinodata.bsm.center.web.action.BaseAction#list()
     */
    @Override
    public String list() throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see com.sinodata.bsm.center.web.action.BaseAction#input()
     */
    @Override
    public String input() throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see com.sinodata.bsm.center.web.action.BaseAction#save()
     */
    @Override
    public String save() throws Exception {
        logger.debug("parameters sended to save:" + topuCatalog.toString());
        topoCatalogService.saveOrUpdate(topuCatalog);
        topuCatalogCache.put(topuCatalog);
        JSONObject jsonObject = JSONObject.fromObject(topuCatalog);
        Struts2Utils.renderJson(jsonObject.toString());
        return null;
    }

    /* (non-Javadoc)
     * @see com.sinodata.bsm.center.web.action.BaseAction#delete()
     */
    @Override
    public String delete() throws Exception {
        if (topuCatalog == null) {
            logger.info("parameters 'topuCatalog' is null:");
            return null;
        }
        logger.debug("parameters sended to delete:" + topuCatalog.toString());
        Long id = topuCatalog.getId();
        topoCatalogService.delete(topuCatalog.getId());
        topuCatalogCache.remove(id);
        topuConfCache.removeByCatalogId(id);
        renderText(RETURN_SUCCESS);
        return null;
    }

    /* (non-Javadoc)
     * @see com.sinodata.bsm.center.web.action.BaseAction#prepareModel()
     */
    @Override
    protected void prepareModel() throws Exception {
        // TODO Auto-generated method stub

    }

    public String updateName() throws Exception {
        //保存要修改的属性值
        String newName = topuCatalog.getName();
        logger.debug("parameters sended to updateName:" + topuCatalog.toString());
        //查询缓存
        topuCatalog = topoCatalogService.findById(topuCatalog.getId());
        //复制bean
        TopuCatalog newTopuCatalog = new TopuCatalog();
        BeanUtils.copyProperties(newTopuCatalog, topuCatalog);
        //对象copy时将Long类型的字段，由null复制成了0。需要再转回来。(业务上没有0值的情况。)
        if (newTopuCatalog != null && newTopuCatalog.getParent() != null && newTopuCatalog.getParent() == 0)
            newTopuCatalog.setParent(null);
        //更新数据库
        newTopuCatalog.setName(newName);
        topoCatalogService.saveOrUpdate(newTopuCatalog);
        //更新缓存
        topuCatalogCache.put(newTopuCatalog.getId(), newTopuCatalog);
        renderText(RETURN_SUCCESS);
        return null;
    }

}
